package com.weblog.weblogapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.weblog.weblogapi.dao.mapper.ArticleMapper;
import com.weblog.weblogapi.dao.mapper.CommentMapper;
import com.weblog.weblogapi.dao.pojo.Article;
import com.weblog.weblogapi.dao.pojo.Comment;
import com.weblog.weblogapi.service.CommentService;
import com.weblog.weblogapi.service.UserService;
import com.weblog.weblogapi.vo.CommentVo;
import com.weblog.weblogapi.vo.Result;
import com.weblog.weblogapi.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private ArticleMapper articleMapper;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Autowired
    private UserService userService;
    @Override
    public Result findCommentByArticleId(int id) {
        LambdaQueryWrapper<Comment> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getArticleId,id);
        queryWrapper.eq(Comment::getLevel,1);
        List<Comment> comments=commentMapper.selectList(queryWrapper);
        List<CommentVo> commentVoList=copyList(comments);
        return Result.success(commentVoList);

    }

    @Override
    public Result writeComment(Comment comment) {
        int parentId = comment.getParentId();
        int articleId = comment.getArticleId();
        LambdaQueryWrapper<Article> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.select(Article::getCountComment);
        queryWrapper.eq(Article::getId,articleId);
        Article article=articleMapper.selectOne(queryWrapper);
        LambdaUpdateWrapper<Article> lambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(Article::getCountComment,article.getCountComment()+1);
        lambdaUpdateWrapper.eq(Article::getId,articleId);
        int j=articleMapper.update(null,lambdaUpdateWrapper);
        if(parentId==0){
            comment.setLevel(1);
        }else {
            comment.setLevel(2);
        }
        comment.setCommentDate(sdf.format(new Date()));
        int i=commentMapper.insert(comment);
        return Result.success(null);
    }

    @Override
    public Result deleteComment(int id) {
        int i=commentMapper.deleteById(id);
        return Result.success(null);
    }

    private List<CommentVo> copyList(List<Comment> comments) {
        List<CommentVo> commentVoList = new ArrayList<>();
        for (Comment comment : comments) {
            commentVoList.add(copy(comment));
        }
        return commentVoList;
    }

    private CommentVo copy(Comment comment) {
        CommentVo commentVo = new CommentVo();
        BeanUtils.copyProperties(comment,commentVo);
//        commentVo.setId(String.valueOf(comment.getId()));
//        Long authorId=comment.getAuthorId();
        String userId=comment.getUserId();
        UserVo userVo=userService.findUserVoById(userId);
        commentVo.setUserVo(userVo);
        int level=comment.getLevel();
        if(level==1){
            int id=comment.getId();
            List<CommentVo> commentVoList=findCommentByParentId(id);
            commentVo.setChildren(commentVoList);
        }
        if(level>1){
            String toUid=comment.getUserId();
            UserVo toUserVo=this.userService.findUserVoById(toUid);
            commentVo.setUserVo(toUserVo);
        }
        return commentVo;

    }

    private List<CommentVo> findCommentByParentId(int id) {
        LambdaQueryWrapper<Comment> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getParentId,id);
        queryWrapper.eq(Comment::getLevel,2);
        return copyList(commentMapper.selectList(queryWrapper));
    }
}
